<?php
$blibiliId = $_GET['id'];//设置b站用户id(不是直播间id)!!!。！！
$url = "https://api.bilibili.com/x/space/acc/info?mid={$blibiliId}&jsonp=jsonp" ;
$res= geturl($url);

//写入检测up主缓存
$file_path = 'cache/'.$blibiliId.".txt";
//判断该up主缓存是否存在 没有则创建缓存文件
if(!file_exists($file_path)){
    $myfile = fopen($file_path, "w") or die("Unable to open file!");
	fwrite($myfile, 'false');
	fclose($myfile);
}	

if($res['data']['live_room']['liveStatus']){
		//主播正在开播
	
		//获取缓存内容
		$content =getFileContent($file_path);
		if($content =='true') {
			//如果缓存中状态已经为正在直播 
			echo '主播正在直播中';
		}else{
			//如果缓存中状态还是未开播
			smsbao(rand(0000,9999));
			cacheUpdate('true',$file_path);
			echo ',已发送短信提醒用户观看直播';
			
		}

		

	    
}else{
	//没开播
	$content=getFileContent($file_path);
	if($content=='false'){
		echo '没开播';
	}else{
		cacheUpdate('false',$file_path);
		echo '主播已下播';
	}
	
	
}


function smsbao($conetent){
	$statusStr = array(
					"0" => "短信发送成功",
					"-1" => "参数不全",
					"-2" => "服务器空间不支持,请确认支持curl或者fsocket，联系您的空间商解决或者更换空间！",
					"30" => "密码错误",
					"40" => "账号不存在",
					"41" => "余额不足",
					"42" => "帐户已过期",
					"43" => "IP地址限制",
					"50" => "内容含有敏感词"
					);
					$smsapi = "http://api.smsbao.com/";
					$user = "*****"; //短信平台帐号
					$pass = md5("*****"); //短信平台密码
					$content="【赛扬科技】你的内容是：{$conetent},收到不用回复";//要发送的短信内容
					$phone = "18953134381";//要发送短信的手机号码
					$sendurl = $smsapi."sms?u=".$user."&p=".$pass."&m=".$phone."&c=".urlencode($content);
					$result =file_get_contents($sendurl) ;
					echo $statusStr[$result];
}
//获取内容
function getFileContent($file_path){
		$str = file_get_contents($file_path);//将整个文件内容读入到一个字符串中
		$str = str_replace("\r\n","<br />",$str);
		return $str;
}
//写入缓存
function cacheUpdate($content,$file_path){
	$myfile = fopen($file_path, "w") or die("Unable to open file!");
	fwrite($myfile, $content);
	fclose($myfile);
	
	
}


function geturl($url){
        $headerArray =array("Content-type:application/json;","Accept:application/json");
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch,CURLOPT_HTTPHEADER,$headerArray);
        $output = curl_exec($ch);
        curl_close($ch);
        $output = json_decode($output,true);
        return $output;
}